package com.newenergy.wisehome.gateway.account;


import android.os.Handler;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.newenergy.wisehome.R;
import com.newenergy.wisehome.common.helper.StorageHelper;
import com.newenergy.wisehome.common.manager.MyThreadManager;
import com.newenergy.wisehome.config.BaseApp;
import com.newenergy.wisehome.gateway.common.BLCommonUtils;
import com.newenergy.wisehome.gateway.manager.BLLocalFamilyManager;
import com.newenergy.wisehome.ui.fragment.BaseFragment;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.broadlink.account.BLAccount;
import cn.com.broadlink.base.BLLoginResult;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends BaseFragment {

    @BindView(R.id.et_name)
    TextInputEditText mEtName;
    @BindView(R.id.et_pwd)
    TextInputEditText mEtPwd;
    @BindView(R.id.btn_login)
    Button mBtnLogin;
    @BindView(R.id.tv_to_register)
    TextView mTvToRegister;
    @BindView(R.id.tv_find_pwd)
    TextView mTvFindPwd;

    @BindView(R.id.tv_page)
    TextView mTvPage;

    private Handler mHandler = new Handler();


    public LoginFragment() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_login;
    }

    @Override
    public void init(View rootView) {
        mTvPage.setText("登录");
//        getPhoneNumber();
    }

//    private void getPhoneNumber() {
//        TelephonyManager tm = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
//        String tel = tm.getLine1Number();//手机号码
//        String imei = tm.getSimSerialNumber();
//        String imsi = tm.getSubscriberId();
//        Log.i(TAG, "getPhoneNumber: 手机号码 = " + tel);
//
//    }


    @OnClick({R.id.btn_back, R.id.tv_back, R.id.btn_login, R.id.tv_to_register, R.id.tv_find_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                doBack();
                break;
            case R.id.tv_back:
                doBack();
                break;
            case R.id.btn_login:

                loginAction();
                break;
            case R.id.tv_to_register:
                startWithPop(new RegisterFragment());
                break;
            case R.id.tv_find_pwd:
//                start(new ForgetPasswordFragment());
                startWithPop(new ResetPasswordFragment());
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void loginAction() {
        String userName = mEtName.getText().toString();
        String password = mEtPwd.getText().toString();
        boolean network = BLCommonUtils.checkNetwork(getContext());
        if (!network) {
            toast("网路不可用！");
            return;
        }
        showLoading("正在登录...");
        MyThreadManager.getInstance().doTask(() -> {
            BLLoginResult loginResult = BLAccount.login(userName, password);
            mHandler.post(() -> notifyLoginResult(loginResult, userName, password));
        });
    }


    private void notifyLoginResult(BLLoginResult loginResult, String userName, String password) {
            dismissLoading();
        if (loginResult != null && loginResult.succeed()) {
            toast("登录成功");
            BaseApp.getmBLUserInfoUnits().login(loginResult);
            String[] accountInfo = {userName, password};
            StorageHelper.setBLAccountInfo(accountInfo);
            BLLocalFamilyManager familyManager = BLLocalFamilyManager.getInstance();
            familyManager.isLogin = true;
            familyManager.updateShareFamilysBLInfo();
            startWithPop(new GatewayManageFragment());

        } else {
            toast("登录失败");
            if (loginResult != null) {
                Log.e(TAG, "登录失败：error = " + loginResult.getError() + "  msg = " + loginResult.getMsg());
            }
        }
    }
}

